package org.example.RobotModel.generator.Android

import org.example.RobotModel.robotDsl.Device
import org.eclipse.emf.common.util.EList

class AndroidGen {
	static def generateAndroidManifest(EList<Device> ds) {
		'''
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="robot"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="robot.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        «FOR d : ds»
        <activity
            android:name=".Action«d.name»"
            /> 
        «ENDFOR»
       
    </application>

</manifest>
		'''
	}

	static def generateMainActivity(Iterable<Device> ds) {
		'''
package robot;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		«FOR d : ds»
		final Button boutton«d.name» = (Button) findViewById(R.id.button«d.name»);
		boutton«d.name».setOnClickListener(new View.OnClickListener() {
			      @Override
			     public void onClick(View v) {
			    	Intent intent = new Intent(MainActivity.this, Action.class);
			  		startActivity(intent);
			     }
			  });
		«ENDFOR»
	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
}
	'''

	}

	static def generateActions(Device d) {
		'''
package robot;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.HashMap;

public class Action«d.name» extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.action);
		«FOR a : d.actions»
		final Button action«a.name» = (Button) findViewById(R.id.button«a.name»);
		action«a.name».setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				HashMap<String, String> params = new HashMap<String, String>();
				«FOR p : a.params»
					EditText «p.name»= (EditText) findViewById(R.id.«p.name»);
					params.put("«p.name»", «p.name».getText().toString());
				«ENDFOR»
				
				Restput rp= new Restput(params);
				try {
					Toast toast = Toast.makeText(getApplicationContext(), "Parametre envoyé!", Toast.LENGTH_SHORT);
					toast.show();
					rp.clickme("http://localhost:8182/«a.name»");
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				

			}
		});
		«ENDFOR»
		
}
   

		
		'''
	}

	static def generateRestPut() {
		'''
package robot;

import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;
import java.util.HashMap;
improt java.util.Iterator;

public class Restput {

 	private HashMap<String,String> params
	
	public Restput(HashMap<String,String> p){
		params = p;
		
	}
	
	public void clickme(String adr) throws Exception{
		Random random = new Random();
		URL url = new URL(adr);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod("PUT");
		connection.setDoOutput(true);
		connection.setRequestProperty("Content-Type", "application/json");
		connection.setRequestProperty("Accept", "application/json");
		OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
		String txtval1= "{";
		Iterator<String> it = params.keySet().iterator;
		int i=0;
		while(it.hasNext()){
				if(i != 0) textval+= ",";
				textval1 +=\"text\" : \""+params.get(it.next())+"\";
				i++;
		}
		txtval1+="}";
		
		System.out.print(txtval1);
		osw.write(String.format(txtval1, random.nextInt(30), random.nextInt(20)));
		osw.flush();
		osw.close();
		System.out.print("fin");
		System.err.println(connection.getResponseCode());
		
	}
}
		'''
	}

	def static generateMainActivityXml(EList<Device> ds) {
		'''
	<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
	«var i = 60»
	«FOR d : ds»	
	    <Button
	    android:id="@+id/button«d.name»"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_alignParentTop="true"
	    android:layout_centerHorizontal="true"
	    android:layout_marginTop="«i»dp"
	    android:text="Device 1" />
        «i = i + 60»
	«ENDFOR»


</RelativeLayout>
		'''
	}

	def static generateActionsXml(Device device) {
		'''
		<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/param2"
        android:layout_alignBottom="@+id/param2"
        android:layout_marginRight="16dp"
        android:layout_toLeftOf="@+id/param2"
        android:text="P1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/param1"
        android:layout_alignLeft="@+id/textView1"
        android:text="P2"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/param1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/param2"
        android:layout_below="@+id/param2"
        android:ems="10" />

    <EditText
        android:id="@+id/param2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_below="@+id/param1"
        android:layout_marginTop="15dp"
        android:text="Action 1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Action 2" />

</RelativeLayout>
		'''
	}

}
